package com.hu.biz.encrypt;

import cn.hutool.core.util.ObjectUtil;
import com.hu.common.util.adm.ReflectUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author qiangt
 * @version 1.0
 * @date 2024/8/31 10:24
 * @description: 加解密处理器工厂类
 */
public class EncryptFactory {
    static {
        Map<String, EncryptProcessor> map = new HashMap<>(16);
        // 取类包名.分割后的第一个作为待扫描包
        String packageName = EncryptProcessor.class.getName().split("\\.")[0];
        Set<Class<? extends EncryptProcessor>> classSet =
            ReflectUtil.scanClassBySuper(packageName, EncryptProcessor.class);
        classSet.forEach(clazz -> {
            EncryptProcessor encryptProcessor = null;
            try {
                encryptProcessor = clazz.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if (ObjectUtil.isNotNull(encryptProcessor)) {
                map.put(encryptProcessor.getAlgorithm(), encryptProcessor);
            }
        });
        beanMap = map;
    }

    static Map<String, EncryptProcessor> beanMap;

    public static EncryptProcessor getBean(String algorithm) {
        return beanMap.get(algorithm);
    }

}
